<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

//===== 首页
//===== 一级导航页面
//===== 二级导航页面
//===== 权限管理页面
//===== 用户管理页面
//===== 密码设置
//===== 服务管理
//===== 清除缓存
//===== 条件管理


//===== 公共区


    private $data_Nav1 = '';        //缓存一二级导航数据
    private $data_AuthGroup = '';   //缓存权限分组数据
    private $UserInfo = '';




//===== 信息管理
    public function service_info_control(){

        $service_list = $this->get_serviceType();
        $this->assign('service_list',$this->get_serviceType());
        $this->display();exit;

    }

        //获取信息列表
        public function service_info_control_infoList(){
            $service_id = I('get.service_id');

            $info_type = I('get.info_type');
            if(!$service_id){
                exit('e1');
            }
            switch($info_type){
                case 'none':
                    $where['info_stat'] = '';
                break;
                case '1':
                    $where['info_stat'] = 1;
                break;
                case '2':
                    $where['info_stat'] = 2;
                break;
                case '3':
                    $where['info_stat'] = 3;
                break;
                case '4':
                    $where['info_stat'] = 4;
                break;
            }
            $m = $this->get_model($service_id);
            $pcount = 10;
            $data = Get_Fpage_Ajax($m,$fields_exchange,$where,$order = 'info_id desc',$pcount);
            foreach($data['list'] as $k=>$v){
                if($v['info_imgs'] == null){
                    $data['list'][$k]['info_imgs'] = 'null';
                }else{
                    $data['list'][$k]['info_imgs'] = json_decode($v['info_imgs'],1);
                }
            }
            $this->assign('data',$data);
           // dump($data);
            $this->display();exit;
        }

        //信息操作处理
        public function service_info_check_edit(){
            $type = I('post.type');
            $info_id = I('post.info_id');
            $service_id = I('post.service_id');
            $uid = I('uid');
            $m = $this->get_model($service_id);
            switch($type){
                case 'allow':
                echo 3;
                    $res = $m->data(array('info_id'=>$info_id,'info_stat'=>2))->save();
                break;
                case 'deny':
                   $res = $m->data(array('info_id'=>$info_id,'info_stat'=>3))->save();
                break;
                case 'del':
                   $res = $m->data(array('info_id'=>$info_id,'info_stat'=>4))->save();
                break;
                case 'recovery':
                   $res = $m->data(array('info_id'=>$info_id,'info_stat'=>1))->save();
                break;
            }
            $path = 'Service/Template/Service_me/'.$uid;
            del_allFiles($path);
            echo 'ok';

        }





//===== 清除缓存
    public function service_clear_cache(){
        $this->display();
    }
    public function service_clear_cache_c(){
        $index='Service/Template/Service_index';
        $issue='Service/Template/Service_issue';
        $me='Service/Template/Service_me/';
        $detail='Service/Template/Service_detail';

        del_allFiles($index);
        del_allFiles($issue);
        del_allFiles($me);
        del_allFiles($detail);
        S(null);
    }




    public function del_file($sid){
        unlink('Service/Template/Service_index/service_index_conditionArea_sub_'.$sid.'.html');
        foreach($this->get_serviceType() as $v){
            if($sid == $v['type_id']){
                $sid = $v['type_name'];
            }
        }
        switch($sid){
            case '求职招聘':$file_name='job';break;
            case '房屋租售':$file_name='house';break;
            case '宠物交易':$file_name='pet';break;
            case '二手物品':$file_name='junk';break;
            case '二手车辆':$file_name='car';break;
            case '寻物寻人':$file_name='lose';break;
            case '出兑信息':$file_name='exchange';break;
        }
        unlink('Service/Template/Service_index/service_index_conditionArea_sub.html');
        unlink("Service/Template/Service_issue/service_issue_serviceList_condition_{$file_name}.html");
    }









    public function _initialize(){
        //var_dump(session('UserInfo'));
        UserCheck();  //校验用户登陆状态
        $this->UserInfo = session('UserInfo');
   /*     if(!session('UserInfo') || !session('uid')){
           // header('location:'.U('Login/login'));exit;
        }else{
        }*/



        //缓存一级导航信息
        if($this->data_Nav1 == ''){
            //获取一级导航列表
            $class_Nav1 = new ModelNav1Controller();
            $this->data_Nav1 = $class_Nav1->Nav1_Get_List();

            //获取二级导航列表
            $class_Nav2 = new ModelNav2Controller();
            $data_Nav2 = $class_Nav2->Nav2_Get_All();
            foreach($this->data_Nav1['list'] as $k=>$v){
                $Nav1_Id = $v['nav1_id'];

                $arr = Array();             
                foreach($data_Nav2 as $v2){
                    $Nav2_Parent_Id =  $v2['nav2_parent'];
                    if($Nav1_Id == $Nav2_Parent_Id){
                        array_push($arr,$v2);
                    }
                }
                foreach($arr as $k3=>$v3){
                    $tmp = '';
                    foreach($arr as $k4=>$v4){
                        if($k4 >= count($arr)-1){
                            break;
                        }
                        if($v4['nav2_order'] > $arr[$k4+1]['nav2_order']){
                            $tmp = $arr[$k4];
                            $arr[$k4] = $arr[$k4+1];
                            $arr[$k4+1] = $tmp;
                        }
                    }
                }
                $this->data_Nav1['list'][$k]['nav2'] = $arr;
            }
        }

   
    }

//===== token 设置
    public function token(){
        if(I('get.token')){
            $name = I('get.token');
        }
        if(I('post.token')){
            $name = I('post.token');
        }
        if(!$name){
            exit;
        }
        echo  Token_Set($name);
    }


//===== 首页
    public function index(){
        $url = 'http://'.$_SERVER['HTTP_HOST'];
        $this->assign('url',$url);

        $user_name = session('UserInfo')['name'];
        $this->assign('user_name',$user_name);
        //一级导航获取
        $this->assign('data_Nav1',$this->data_Nav1);
    	$this->display();
    }


//===== 一级导航页面
    public function admin_nav1(){
        $this->assign('data_Nav1',$this->data_Nav1);
        $this->assign('data_Nav1_Count',count($this->data_Nav1['list']));
        $this->display();
    }


//===== 二级导航页面
    public function admin_nav2(){
        $this->assign('data_Nav1',$this->data_Nav1);
        $this->display();
    }

        //二级导航列表
        public function admin_nav2_list(){
            $nav2_parent = I('get.nav2_parent');
            $where['nav2_parent']=$nav2_parent;
            $cls_Nav2 = new ModelNav2Controller();
            $data_nav2 = $cls_Nav2->Nav2_Get($where);
          // var_dump($data_nav2);
            $this->assign('data_nav2',$data_nav2);
            $this->assign('data_nav2_count',count($data_nav2['list']));
            $this->display();
        }







//===== 密码设置
    public function admin_user_setting(){
        $this->display();
    }

        public function admin_user_setting_sub(){
            $Old_Pwd = I('post.Old_Pwd');
            $New_Pwd = I('post.New_Pwd');
            $New_Pwd2 = I('post.New_Pwd2');
            if(!$Old_Pwd || !$New_Pwd || !$New_Pwd2){
                echo "<script>alert('不得为空！');history.go(-1);</script>";exit;
            }
            if($New_Pwd2 != $New_Pwd){
                echo "<script>alert('新密码不一致！');history.go(-1);</script>";exit;
            }
            $m = M('Think_auth_user');
            $res_User = $m->where(array('id'=>$this->UserInfo['id']))->find();
            if(Encrypt($Old_Pwd) != $res_User['pwd']){
                echo "<script>alert('原密码不正确！');history.go(-1);</script>";exit;
            }
            if(Encrypt($New_Pwd) == $res_User['pwd']){
                echo "<script>alert('新旧密码相同！');history.go(-1);</script>";exit;
            }
            $st = $m->where(array('id'=>$this->UserInfo['id']))->data(array('pwd'=>Encrypt($New_Pwd)))->save();
            if($st){
                echo "<script>alert('密码修改成功！');history.go(-1);</script>";exit;
            }else{
                echo "<script>alert('密码修改失败！');history.go(-1);</script>";exit;
            }
        }



//===== 服务管理
    public function service_type(){
        $m = M('Service_type');
        $res = Get_Fpage($m,$fields='',$where='',$order = 'type_order',$pcount = 10);
        $this->assign('res',$res);
        $this->assign('res_list_count',count($res['list']));
        $this->display();        
    }

        //对服务的增删操作时，删除相应缓存服务的静态页面
        public function serviceId_delFile(){
            unlink('Service/Template/Service_index/service_index_conditionArea.html');
            unlink('Service/Template/Service_issue/service_issue_serviceList.html');
            unlink('Service/Template/Service_me/service_me_content.html');
            S('service_type',null);
        }
        //添加服务的处理
        public function service_type_sub(){
            $name = I('post.name');
            $order = I('post.order');
            $table_name = I('post.table_name');
            $token = I('post.token');
            if(!Token_V('service_type_sub',$token)){
                echo "<script>alert('非法访问');location.href='".U('Service_type')."';</script>";
            }
            if(!$name || !$order){
                echo "<script>alert('不得为空！');location.href='".U('Service_type')."';</script>";exit;
            }
            if(!preg_match('/^[1-9][0-9]{0,2}$/',$order)){
                echo "<script>alert('序号只能为三位以下正整数！');location.href='".U('Service_type')."';</script>";exit;
            }
            $m = M('Service_type');
            $res = $m->where(array('type_name'=>$name))->find();
            if($res){
                echo "<script>alert('服务名以存在！');location.href='".U('Service_type')."';</script>";exit;
            }
            $st = $m->data(array('type_name'=>$name,'type_order'=>$order,'table_name'=>$table_name))->add();
            if($st > 0){
                $this->serviceId_delFile();
                echo "<script>alert('添加成功！');location='".U('service_type')."'</script>";exit;
            }else{
                echo "<script>alert('添加失败');location='".U('service_type')."'</script>";exit;
            }
        }

        public function service_del(){
            $token = I('get.token');
            $sid = I('get.sid');
            if(I('get.pnow')){
                $pnow = "?pnow=".I('get.pnow');
            }else{
                $pnow = '';
            }
            if(!$token || !$sid){
                echo "<script>alert('参数丢失！');location='".U('service_type').$pnow."'</script>";exit;
            }
            if(!Token_V('service_del',$token)){
                echo "<script>alert('非法访问！');location='".U('service_type').$pnow."'</script>";exit;
            }
            $m = M('Service_type');
            $st = $m->delete($sid);
            if($st){
                $this->serviceId_delFile();
                echo "<script>alert('删除成功！');location='".U('service_type').$pnow."'</script>";exit;
            }else{
                echo "<script>alert('删除失败！');location='".U('service_type').$pnow."'</script>";exit;
            }
        }



//===== 条件管理
    public function service_cond(){

        $this->assign('res_Type',$this->get_serviceType());
        $this->display();
    }
        //异步加载条件列表
        public function service_cond_list(){
            $sid = I('get.sid');
            $m_Cond = M('Service_condition');
            $res_Cond = $m_Cond->where(array('cond_service_id'=>$sid))->order('cond_id')->select();
            $arr_lv1 = $arr_lv2 = array();
            foreach($res_Cond as $v){
                if($v['cond_level'] == 1){
                    $arr_lv1[] = $v;
                }else{
                    $arr_lv2[] = $v;
                }
            }
            foreach($arr_lv1 as $k=>$v){
                foreach($arr_lv2 as $k2=>$v2){
                    if($v['cond_id'] == $v2['cond_parent']){
                        $arr[] = $v2;
                    }
                }
                $arr_lv1[$k]['lv2'] = $arr;
                $arr = array();
            }
          //  dump($arr_lv1);
            $this->assign('res_Cond',$arr_lv1);
            $this->display();
        }


        public function service_cond_lv1_sub(){
            $name = I('post.name');
            $name_form = I('post.name_form');
            $token = I('post.token');
            $service_id = I('post.service_id');

            if(!$name || !$name_form){
                echo "<script>alert('条件名不得为空！');location.href='service_cond';</script>";exit;
            }
            if(!$token || !Token_V('service_cond_sub_lv1',$token)){
                echo "<script>alert('非法访问！');location.href='service_cond';</script>";exit;
            }
            $m_Cond = M('Service_condition');

            $data['cond_service_id'] = $service_id;
            $data['cond_name'] = $name;
            $data['cond_name_form'] = $name_form;
            $st = $m_Cond->data($data)->add();
            if($st > 0){
                $this->del_file($service_id);
                echo "<script>alert('添加成功！');location.href='service_cond';</script>";exit;
            }else{
                echo "<script>alert('添加失败！');location.href='service_cond';</script>";exit;
            }
        }

        public function service_cond_edit(){
            if(!Token_V('CondEdit',I('token'))){
                exit('e1');     //非法访问！
            }
            $cid = I('post.cid');
            $sid = I('post.sid');
            $view = I('post.view');
            if(!$cid || !$view){
                exit('e2');     //缺少参数！
            }
            $m = M('Service_condition');
            $st = $m->data(array('cond_view'=>$view))->where(array('cond_id'=>$cid))->save();
            if($st){
                $this->del_file($sid);
                echo 'ok';
            }else{
                echo 'e3';
            }
        }


        public function service_cond_del(){
            $cid = I('post.cid');
            $sid = I('post.sid');
            $token = I('post.token');
            if(!$cid || !$token){
                exit("e1"); //缺少参数
            }
            if(!Token_V('CondLv1Del',$token)){
                exit('e2'); //非法访问
            }
            $m = M('Service_condition');
            $st = $m->delete($cid);
            if($st){
                $st2 = $m->where(array('cond_parent'=>$cid))->delete();
                $this->del_file($sid);
                exit('ok');
            }else{
                exit('e3'); //删除失败
            }
        }


        public function service_cond_lv2_sub(){
            $sid = I('post.sid');
            $cid = I('post.cid');
            $name = I('post.name');
            $token = I('post.token');
            if(!$sid || !$cid || !$name || !$token){
                exit('e1');     //缺少参数!
            }
            if(!Token_V('CondLv2Add',$token)){
                exit('e2');     //非法访问!
            }
            $m = M('Service_condition');
            $data['cond_service_id'] = $sid;
            $data['cond_name'] = $name;
            $data['cond_level'] = 2;
            $data['cond_parent'] = $cid;
            $st = $m->data($data)->add();
            if($st){
                $this->del_file($sid);
                exit('ok'); //添加成功！
            }else{
                exit('e3'); //删除失败!
            }
        }





//===== 用户管理
    public function user_control(){
        $this->display();
    }

        //展示用户列表
        public function user_control_userList(){
            $m = M('Tp_user');

            //获取设置用户分类
            $userType = I('get.userType');
            $where = '';
            switch($userType){
                case 1:
                    $where = array('user_stat'=>1); 
                    break;
                case 2:
                    $where = array('user_stat'=>2); 
                    break;
            }

            //设置分页码信息
            $pageNow = I('get.pageNow',1); //当前页码
            $count = $m->where($where)->count();   //总条数
            $pageSize = 10;         //每页条数
            $pageNum = ceil($count / $pageSize);    //总页数

            if($pageNum > 1){
                $start = $pageNow - 2;
                $start = $start > 0 ? $start : 1;
                $end = $pageNow + 2;
                $end = $end > $pageNum ? $pageNum : $end;
                $pageCode = '';
                $pageCode .= "<a style='cursor:pointer;border:1px solid;color:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow=1;get_user_list();'>首页</a>";
                $pageLast = $pageNow-1>0?$pageNow-1:1;
                $pageCode .= "<a style='cursor:pointer;border:1px solid;color:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow={$pageLast};get_user_list();'>上页</a>";
                for($i=$start;$i<=$end;$i++){
                    if($pageNow == $i){
                        $pageCode .= "<a style='cursor:pointer;border:1px solid;color:#fff;background:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow={$i};get_user_list();'>$i</a>";
                    }else{
                        $pageCode .= "<a style='cursor:pointer;border:1px solid;color:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow={$i};get_user_list();'>$i</a>";
                    }
                }
                $pageNext = $pageNow+1<$pageNum?$pageNow+1:$pageNum;
                $pageCode .= "<a style='cursor:pointer;border:1px solid;color:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow={$pageNext};get_user_list();'>下页</a>";
                $pageCode .= "<a style='cursor:pointer;border:1px solid;color:blue;margin:5px;padding:3px 5px' onclick='sessionStorage.pageNow={$pageNum};get_user_list();'>尾页</a>";
                $this->assign('pageCode',$pageCode);
            }
            $limit_start = ($pageNow-1)*$pageSize;  
            $data = $m->where($where)->limit($limit_start,$pageSize)->select();
            $this->assign('data',$data);
            $this->display();
        }



        //用户的封禁解封操作
        public function isDeny(){
            $uid = I('post.uid');
            $stat = I('post.stat');
            if(!$uid || !$stat){
                exit;
            }
            if($stat == 1){
                $stat = 2;
            }else{
                $stat = 1;
            }
            $data = array('user_stat'=>$stat);
            $m = M('Tp_user');
            echo $m->where(array('user_id'=>$uid))->save($data); //成功返回1

        }



        //查看用户发布的信息
        public function user_control_userList_cat_infos(){
            $uid = I('post.uid');
            if(!$uid){
                exit;
            }
            $m = M('Tp_user_issue_infos');
            $res = $m->where(array('uid'=>$uid))->find();
            $data =json_decode($res['infos'],1);
            $service_id_arr = array();
            $service_info_arr = array();
            foreach($data as $v){
                if(!in_array($v['service_id'],$service_id_arr)){    //判断并将用户信息中的服务id归类单独存放到一个数组中
                    $service_id_arr[] = $v['service_id'];
                }
                if(in_array($v['service_id'],$service_id_arr)){     //将所有信息归类，按照服务id 将信息存放在各服务id下的二级数组中
                    $service_info_arr[$v['service_id']]['info_ids'][] = $v['info_id'];
                }
                
            }
            $data_arr = array();
            foreach($service_info_arr as $k=>$v){
                $m = $this->get_model($k);
                $where['info_id']=array('in',$v['info_ids']);
                $res = $m->where($where)->select();
                foreach($res as $v){
                    $data_arr[] = $v;
                }
         
            }
          echo '<pre>';
            //var_dump($data_arr);
          echo '</pre>';
            $this->assign('data',$data_arr);
            $this->display();
        }


        //查看用户登陆信息
        public function user_control_userList_cat_login_infos(){
            $uid = I('post.uid');
            $m = M('tp_user_login_info');
            $res = Get_Fpage_Ajax($m,'',array('uid'=>$uid),'ltime desc',15);

          //  $res = $m->where(array('uid'=>$uid))->select();
            echo '<pre>';
          //  var_dump($res);
            echo '</pre>';
            $this->assign('data',$res);
            $this->display();
        }








//===== 公共区
        //获取服务列表 
        public function get_serviceType(){
            if(!S('service_type')){
                $m_Type = M('Service_type');
                $service_type = $m_Type->order('type_order desc')->select();
                S('service_type',$service_type);
            }
            return S('service_type'); 
        }

        //根据服务id 获取相应的实例
        public function get_model($service_type_id){
            foreach (S('service_type') as $v) {
               if($service_type_id == $v['type_id']){
                    return M($v['table_name']);
               }
            }
        }



  
}